package com.wg.wuge.tulin.jmm;

import java.util.concurrent.atomic.AtomicReference;

public class AtomicReferenceTest {


    public static void main(String[] args) {
        User user1 = new User("张三", 18);
        User user2 = new User("李四", 19);
        User user3 = new User("王五", 20);

        AtomicReference<User> atomicReference = new AtomicReference<>();
        atomicReference.set(user1);

        atomicReference.compareAndSet(user1, user2);
        System.out.println(atomicReference.get());

        atomicReference.compareAndSet(user1, user3);
        System.out.println(atomicReference.get());

    }
}

class User {
    String name;
    Integer age;
    public User(String name, Integer age) {
        this.age = age;
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
